package main.car;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import main.model.ElaModel;
import main.model.ElaRepository;
import main.model.Model;
import main.model.ModelDao;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

//@Configuration
@Slf4j
public class HttpInfo {
    private static volatile Integer id = 10;
    @Resource
    private ModelDao modelDao;
    @Resource
    private ElaRepository elaRepository;
    @Value("https://www.yidaiyilu.gov.cn")
    private String baseUrl;

    @Value("https://www.yidaiyilu.gov.cn/search/newSearch.jsp?q=%E7%99%BD%E4%BF%84%E7%BD%97%E6%96%AF&t_id=290&pageSize=15&qIndex=1&siteId=CMSydylgw")
    private String url;

    @Resource
    private WebDriver driver;
    //    @Scheduled(fixedDelay = 2000000)
    public void main() {
        String html = getHtml(driver, url);
        if (html == null) return;
        Document document = Jsoup.parse(html);
        Elements results = document.select("a[style=font-size: 18px;color: #14038f;font-family: 黑体;]");
        List<String> urls = new ArrayList<>();
        for (Element result : results) {
            urls.add(baseUrl + result.attr("href"));
        }
        getInfo(driver, urls);
    }

    public String getHtml(WebDriver driver, String url) {
        driver.get(url);
        driver.get(url);
        return driver.getPageSource();
    }

    public void getInfo(WebDriver driver, List<String> urls) {
        log.info("=====> start...All,list ==>{}", urls.size());
        Document document;
        Random random = new Random();
        int count = 1;
        for (String s : urls) {
            log.info("=====> start...{} ==> {}", s, count);
            count++;
            try {
                int rand = random.nextInt(3) + 1;
                Thread.sleep(rand * 1000);
                String html = getHtml(driver, s);
                document = Jsoup.parse(html);
                //标题
                Elements elements = document.select("h1.main_content_title");
                ElaModel model = new ElaModel();
                model.setCreateTime(new Date());
                model.setId(id);
                id++;
                if (!elements.isEmpty()) {
                    System.out.println(elements.get(0).text());
                    model.setTitle(elements.get(0).text());
                }
                elements = document.select("div.szty").get(0).children();
                for (int i = 0; i < elements.size(); i++) {
                    if (i == 0) {
                        //时间
                        System.out.println(elements.get(i).text());
                        model.setTime(elements.get(i).text());
                    } else if (i == 1) {
                        //来源
                        System.out.println(elements.get(i).text());
                        model.setSource(elements.get(i).text());
                    } else {
                        //作者
                        System.out.println(elements.get(i).text());
                        model.setAuthor(elements.get(i).text());
                    }
                }
                //内容
                elements = document.select("div.info_content");
                if (!elements.isEmpty()) {
                    System.out.println(elements.get(0).text());
                    String content = elements.get(0).html();
                    String[] con = content.split("src=\"");
                    for (int i = 1; i < con.length; i++) {
                        con[i] = baseUrl + con[i];
                    }
                    content = String.join("src=\"", con);
                    model.setContent(content);
                }
                //编辑
                elements = document.select("div.editor");
                if (!elements.isEmpty()) {
                    System.out.println(elements.get(0).text());
                    model.setEditor(elements.get(0).text());
                }
                elaRepository.save(model);
                log.info("=====> end...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
